package com.kischang.blog.dao;

import com.kischang.blog.model.TitleMenu;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class TitleMenuDao {
	@Autowired
	private SessionFactory sessionFactory;

	@SuppressWarnings("unchecked")
	public List<TitleMenu> getAllParentMenu(){
		Session session = sessionFactory.getCurrentSession();
		return session.createQuery("FROM com.kischang.blog.model.TitleMenu as m where m.child=false")
			.list();
	}

	public TitleMenu getMenuById(int mid) {
		return (TitleMenu) sessionFactory.getCurrentSession().get(TitleMenu.class, mid);
	}

    public TitleMenu getTitleMenuByName(String name) {
        Session session = sessionFactory.getCurrentSession();
        return (TitleMenu) session.createQuery("FROM com.kischang.blog.model.TitleMenu as m where m.name=:name")
                .setString("name", name)
                .uniqueResult();
    }
}
